package com.markedquilt.SwordForOffer;
/*
 * PackageName: com.markedquilt.SwordForOffer
 * ClassName:
 * Author:   zhang
 * Date:     Created in 2020/11/16
 * Description:
 * History:
 * <author>        <time>             <version>          <desc>
 * 作者姓名         修改时间            版本号              描述
 * zhang           2020/11/16      1.0
 */

import java.util.Arrays;

/**
 * Title:
 * Project: LeetcodeProblem
 *
 * @author ZhangKe
 * Created on 2020/11/16
 */
public class _replaceSpace05 {
    public static String replaceSpace(String s) {
        if ("".equals(s) || s.length()==0){
            return "";
        }
        int originLength = s.length();
        char[] array = new char[originLength];
        int count=0;
        for (int i = 0; i < originLength; i++) {
            if (s.charAt(i) == ' '){
                count++;
            }
            array[i] = s.charAt(i);
        }
        int newArrayLength = s.length() + count*2;
        char[] newArray = new char[newArrayLength];
        newArrayLength = newArrayLength-1;
        for (int i = array.length - 1; i >= 0; i--) {
            if (array[i] == ' '){
                newArray[newArrayLength--] = '0';
                newArray[newArrayLength--] = '2';
                newArray[newArrayLength--] = '%';
            }else{
                newArray[newArrayLength--] = array[i];
            }
        }
        return String.valueOf(newArray);
    }

    public static void main(String[] args) {
        String s = replaceSpace("abc cba 123 321 asf ");
        System.out.println(s);
    }
}
